import axios from 'axios';
let qs = require('qs');

let host = window.location.host

let baseUrl = `http://localhost:8077/table`;
/**
 * get 请求
 */
 const httpGet = function (url = '', data = {}) {
    const instance = axios.create({
      baseURL: baseUrl,
      withCredentials: false,
      headers: {
        'Accept': 'application/json;charset=UTF-8',
        'content-type': 'application/json'
      }
    });
    return instance.get(url, {
      params: data
    }).then((result) => {
      var res = result.data;
      if(res && res.code === 0) {
        return Promise.resolve(res);
      } else {
        if (res.code === 15000){
          this.$message({
            message: '系统异常',
            type: 'warning'
          });
          return res;
        } else if (res.code === 10000) {
          this.$message({
            message: res.message,
            type: 'warning'
          });
          return res;
        }
        return Promise.resolve(res);
      }
    }).catch((error) => {
      let res = error.response
      if (res.status === 400 || res.status === 401) {
        if (res.data) {
          this.$message({
            message: res.data.message,
            type: 'warning'
          });
        }
      }
    })
  }
  
  /**
   * post 请求
   */
  const httpPost = function (url = '', data = {}) {
    const instance = axios.create({
      baseURL: baseUrl,
      headers: {
        'Accept': 'application/json;charset=UTF-8',
        'content-type': 'application/json'
      }
    });
    return instance.post(url, JSON.stringify(data)).then((result) => {
      var res = result.data;
      if(res && res.code === 0) {
        return Promise.resolve(res);
      }
    }).catch((error) => {
      this.$message({
          message: error,
          type: 'warning'
      });
    })
  }

export {
  baseUrl,
  httpGet,
  httpPost
}
